package com.magnificent.atlantis.server.ui.panels.logger;

import com.magnificent.atlantis.server.event.Event;
import com.magnificent.atlantis.server.event.EventDispatcher;
import com.magnificent.atlantis.server.event.EventDispatcherListener;
import com.magnificent.atlantis.server.event.EventType;
import com.magnificent.atlantis.server.ui.panels.common.AbstractServerPanel;
import com.magnificent.helpers.CommonUIHelper;

import javax.swing.*;
import java.awt.*;

/**
 * User: Babanin
 * Date: Oct 2, 2009
 * Time: 11:58:56 PM
 */
public class LoggerPanel extends AbstractServerPanel implements EventDispatcherListener {
    private JTextPane loggerTextPane;

    public LoggerPanel() {
        initializeControls();
        initializeUI();

        EventDispatcher.getInstance().addListener(this);
    }

    private void initializeUI() {
        setLayout(new BorderLayout());

        add(
                CommonUIHelper.packInShiftedPanel(
                        new JScrollPane(loggerTextPane), 5, 5, 5, 5
                ), BorderLayout.CENTER
        );
    }

    public void newEvent(EventType type, Event event) {
        String message = event.toString();

        loggerTextPane.setEditable(false);
        loggerTextPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));

        loggerTextPane.setText(loggerTextPane.getText() + "\n" + message);
        loggerTextPane.setCaretPosition(loggerTextPane.getCaretPosition() + message.length());
    }

    private void initializeControls() {
        loggerTextPane = new JTextPane();
        loggerTextPane.setEditable(false);
    }
}
